package org.shopping;

import org.shopping.database.DB;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "register", value = "/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/jsp/register.jsp").forward(request, response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String name = request.getParameter("name");
        String idCard = request.getParameter("id_card");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");
        if (name.length()>1){
            if (idCard.length()==18){
                if (username.length()>4){
                    if (password.length()>4){
                        if (phone.length()==11){
                            String sql="insert into customers(name,id_card,username,password,phone) values(?,?,?,?,?)";
                            DB.executeUpdate(sql,name,idCard,username,password,phone);
                            out.println("<script>alert('注册成功，请登录！');location.href='/login';</script>");
                        }else{
                            out.println("<script>alert('联系电话长度不符合要求，要求11位！');history.back();</script>");
                            return;
                        }
                    }else{
                        out.println("<script>alert('密码长度不符合要求，至少4位！');history.back();</script>");
                        return;
                    }
                }else{
                    out.println("<script>alert('用户名长度不符合要求，至少4位！');history.back();</script>");
                    return;
                }
            }else{
                out.println("<script>alert('身份证号长度不符合要求，要求18位！');history.back();</script>");
                return;
            }
        }else{
            out.println("<script>alert('名字长度不符合要求，至少两位！');history.back();</script>");
            return;
        }

    }
}
